package com.soxing.braindata.service.sync;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soxing.braindata.dao.entity.Research;
import com.soxing.braindata.dao.entity.ResearchExt;
import com.soxing.braindata.dao.service.IResearchExtService;
import com.soxing.braindata.dao.service.IResearchService;
import com.soxing.braindata.domain.enumeration.FunTypeEnum;
import com.soxing.braindata.domain.response.sync.SyncResponse;
import com.soxing.braindata.domain.rpc.OriginSubject;
import com.soxing.common.utils.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 同步课题服务
 */
@Slf4j
@Service("syncSubjectService")
public class SyncSubjectServiceImpl implements ISyncService<OriginSubject> {
    @Resource
    private IResearchService researchService;
    @Resource
    private IResearchExtService researchExtService;

    @Override
    public SyncResponse syncData(OriginSubject originSubject) {
        try {
            Integer funType = originSubject.getFunType();
            String subjectPkId = originSubject.getSubjectPkId();
            if (funType == null || subjectPkId == null) {
                log.error("funType或subjectPkId为空。funType:{},subjectPkId:{}", funType, subjectPkId);
                return SyncResponse.fail("funType或subjectPkId为空");
            }
            sync(originSubject);
            return SyncResponse.success();
        } catch (Exception e) {
            log.error("同步课题服务异常", e);
            return SyncResponse.fail();
        }
    }

    @Transactional
    void sync(OriginSubject originSubject) {
        researchService.remove(new LambdaQueryWrapper<Research>().eq(Research::getSubjectPkId, originSubject.getSubjectPkId()));
        researchExtService.remove(new LambdaQueryWrapper<ResearchExt>().eq(ResearchExt::getSubjectPkId, originSubject.getSubjectPkId()));
        Integer funType = originSubject.getFunType();
        if (funType == FunTypeEnum.DELETE.getFunType()) {
            return;
        }
        Research research = createResearch(originSubject);
        ResearchExt researchExt = createResearchExt(originSubject);
        researchService.save(research);
        researchExtService.save(researchExt);
    }

    private Research createResearch(OriginSubject originSubject) {
        Research research = new Research();
        research.setSubjectPkId(originSubject.getSubjectPkId());
        research.setSubjectCode(originSubject.getSubjectCode());
        research.setResearchName(originSubject.getSubjectName());
        research.setResearchSimpleName(originSubject.getSubjectSimpleName());
        research.setPiName(originSubject.getUserAccount());
        research.setVpInfo(originSubject.getVpInfo());
        if(originSubject.getType()==null){
            research.setType(0);
        }else{
            research.setType(originSubject.getType());
        }
        return research;
    }

    private ResearchExt createResearchExt(OriginSubject originSubject) {
        ResearchExt researchExt = new ResearchExt();
        researchExt.setSubjectPkId(originSubject.getSubjectPkId());
        researchExt.setState(originSubject.getSubjectStatus());
        researchExt.setAgencyCode(originSubject.getAgencyCode());
        researchExt.setUserAccount(originSubject.getUserAccount());
        researchExt.setCreatePin(originSubject.getCreatePkId());
        researchExt.setStartTime(DateUtil.stringToDate(originSubject.getStartTime(), DateUtil.DATEFORMAT_DAY));
        researchExt.setPlanFinishTime(DateUtil.stringToDate(originSubject.getPlanFinishTime(), DateUtil.DATEFORMAT_DAY));
        researchExt.setLogoUrl(originSubject.getLogoUrl());
        researchExt.setPlanCenterNum(new Integer(originSubject.getPlanCenterNum()));
        researchExt.setPlanNumber(new Integer(originSubject.getPlanNumber()));
        return researchExt;
    }
}
